package com.villaday.idf.custom.pop;

import android.content.Context;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.villaday.idf.R;
import com.villaday.idf.adapter3.FaPiaoTitleHistoryAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.model.FaPiaoTitleEntity;
import com.villaday.idf.util.DbFaPiaoTitleUtils;

import java.util.List;

import razerdp.basepopup.BasePopupWindow;

public class TipsUseDBPop extends BasePopupWindow implements View.OnClickListener {

    private Context mContext;
    private BaseActivity activity;
    private String oldContent;
    private final DbFaPiaoTitleUtils dbInstance;
    private List<FaPiaoTitleEntity> faPiaoTitleEntities;
    private final FaPiaoTitleHistoryAdapter faPiaoTitleHistoryAdapter;
    private final LinearLayout ll_history;

    public TipsUseDBPop(Context context, final BaseActivity activity, String oldContent) {
        super(context, WRAP_CONTENT, WRAP_CONTENT);
        mContext = context;
        this.activity = activity;
        this.oldContent = oldContent;
        final EditText et_title = findViewById(R.id.et_title);

        setClipChildren(false);
        setAutoShowInputMethod(et_title, true);
        setPopupGravity(Gravity.BOTTOM);
        setBackground(0);
        setOutSideDismiss(true);
        setPopupFadeEnable(false);
        //setBackgroundColor(Color.parseColor("#B3000000"));
        setBlurBackgroundEnable(false);
        setAlignBackground(false);

        dbInstance = DbFaPiaoTitleUtils.getInstance();
        et_title.setText(oldContent);

        RecyclerView rv = findViewById(R.id.rv);
        ll_history = findViewById(R.id.ll_history);


        et_title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                activity.mRxManager.post("poptextchange", s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        rv.setLayoutManager(new LinearLayoutManager(context));
       /* DbFaPiaoTitleUtils.getInstance().insertOrReplace("sdfakj");
        DbFaPiaoTitleUtils.getInstance().insertOrReplace("庐阿达撒世纪东方哈打发时间客服回复哈斯");
        DbFaPiaoTitleUtils.getInstance().insertOrReplace("三十三发");
        DbFaPiaoTitleUtils.getInstance().insertOrReplace("非的故事");
*/
        faPiaoTitleHistoryAdapter = new FaPiaoTitleHistoryAdapter(context, this);
        rv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayout.HORIZONTAL, false));
        rv.setAdapter(faPiaoTitleHistoryAdapter);
        faPiaoTitleHistoryAdapter.setFullScreen(true);

        faPiaoTitleEntities = dbInstance.queryDownAll();
        if (faPiaoTitleEntities == null || faPiaoTitleEntities.size() == 0) {
            ll_history.setVisibility(View.GONE);
        } else {

            faPiaoTitleHistoryAdapter.setData(faPiaoTitleEntities);

        }
        faPiaoTitleHistoryAdapter.setOnItemClickListener(new AbsRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                et_title.setText(faPiaoTitleEntities.get(position).getContent());
                activity.mRxManager.post("taxpayerNumber", faPiaoTitleEntities.get(position).getTaxpayerNumber());


                Selection.setSelection(et_title.getText(),et_title.getText().toString().length());
            }
        });


    }


    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.tips_use_db_data);
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        dbInstance.deleteDowninfo(faPiaoTitleEntities.get(position));
        faPiaoTitleEntities = dbInstance.queryDownAll();
        faPiaoTitleHistoryAdapter.setData(faPiaoTitleEntities);
        if (faPiaoTitleEntities.size()==0){
            ll_history.setVisibility(View.GONE);
        }



    }
}
